package com.qtp.generator.controller;

import com.qtp.generator.utils.GtpCodeUtil;
import com.qtp.generator.views.DefaultGeneratorParam;
import com.qtp.generator.views.GeneratorParam;
import com.qtp.generator.utils.CodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 代码生成器Controller
 *
 * @author xiad
 * @date 2020-08-07
 */
@Slf4j
@RestController
public class CodeGeneratorController {

    @Autowired
    private DefaultGeneratorParam defaultGeneratorParam;

    @GetMapping("/")
    public ModelAndView index(ModelAndView model){
        model.addObject("gen", defaultGeneratorParam);
        model.setViewName("index");
        return model;
    }

    @GetMapping("/gtp")
    public ModelAndView indexGtp(ModelAndView model){
        model.addObject("gen", defaultGeneratorParam);
        model.setViewName("gtpIndex");
        return model;
    }

    @PostMapping("doGen")
    public ModelAndView doGen(GeneratorParam generatorParam, ModelAndView model) {
        log.info("生成代码参数：{}", generatorParam.toString());

        model.addObject("gen", generatorParam);

        try {
            CodeUtil.generator(generatorParam);
            model.addObject("message", "成功");
        } catch (Exception e) {
            model.addObject("message", e.getLocalizedMessage());
        }
        model.setViewName("index");
        return model;
    }


    @PostMapping("/doGtpGenerator")
    public ModelAndView gtpGenerator(GeneratorParam generatorParam, ModelAndView model) {
        log.info("生成代码参数：{}", generatorParam.toString());

        model.addObject("gen", generatorParam);

        try {
            GtpCodeUtil.generator(generatorParam);
            model.addObject("message", "成功");
        } catch (Exception e) {
            model.addObject("message", e.getLocalizedMessage());
        }
        model.setViewName("gtpIndex");
        return model;
    }

}
